﻿using N_m3u8DL_RE.Enums;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System.Text;
using VideoTools.vo;
using TaskStatus = VideoTools.vo.TaskStatus;

namespace VideoTools.Tasks
{
    public class DownloadM3u8TaskItem : NotifyPropertyBase, IComparable<DownloadM3u8TaskItem>
    {
        [JsonIgnore]
        public volatile bool ExitTask = false;

        private Uri _RequestUrl;
        public Uri RequestUrl
        {
            get => _RequestUrl;
            set { SetProperty(ref _RequestUrl, value); }
        }
        private string _Command;
        [JsonIgnore]
        public string Command
        {
            get => _Command;
            set { SetProperty(ref _Command, value); }
        }
        [JsonIgnore]
        public CancellationTokenSource CancellationTokenSource { get; set; } =
           new CancellationTokenSource();
        private bool _killed = false;
        [JsonIgnore]
        public bool Killed
        {
            get => _killed;
            set { SetProperty(ref _killed, value); }
        }

        private string _VideoName;
        public string VideoName
        {
            get => _VideoName;
            set { SetProperty(ref _VideoName, value); }
        }

        private EncryptMethod _Method;
        public EncryptMethod Method
        {
            get => _Method;
            set { SetProperty(ref _Method, value); }
        }
        private string _Iv;
        public string Iv
        {
            get => _Iv;
            set { SetProperty(ref _Iv, value); }
        }
        private string _Key;
        public string Key
        {
            get => _Key;
            set { SetProperty(ref _Key, value); }
        }

        private string _FrameSize;
        public string FrameSize
        {
            get => _FrameSize;
            set { SetProperty(ref _FrameSize, value); }
        }

        private ListBoxLineBackground _BgColour;

        [JsonIgnore]
        public ListBoxLineBackground BgColour
        {
            get => _BgColour;
            set { SetProperty(ref _BgColour, value); 
            }
        }

        private TaskStatus _status;

        [JsonIgnore]
        public TaskStatus Status
        {
            get => _status;
            set { SetProperty(ref _status, value); }
        }


        private string _Segment;
        public string Segment
        {
            get => _Segment;
            set { SetProperty(ref _Segment, value); }
        }

        private string _DownloadBytes;

        public string DownloadBytes
        {
            get => _DownloadBytes;
            set { SetProperty(ref _DownloadBytes, value); }
        }

        private string _Speed;

        [JsonIgnore]
        public string Speed
        {
            get => _Speed;
            set { SetProperty(ref _Speed, value); }
        }

        private int _progress = 0;

        [JsonIgnore]
        public int Progress
        {
            get => _progress;
            set
            {
                double d = _progress;
                Progress1 = d / 100;
                SetProperty(ref _progress, value);
            }
        }
        private double _progress1;

        [JsonIgnore]
        public double Progress1
        {
            get => _progress1;
            set { SetProperty(ref _progress1, value); }
        }

        [JsonIgnore]
        private ObservableCollection<LogParams> _Logs = new ObservableCollection<LogParams>();

        [JsonIgnore]
        public ObservableCollection<LogParams> Logs
        {
            get => _Logs;
            set { SetProperty(ref _Logs, value); }
        }

        [JsonIgnore]
        public Action TaskAction { get; set; }

        public void Cancel()
        {
            Killed = true;
            CancellationTokenSource?.Cancel();
            Status = TaskStatus.Failed;
            ExitTask = true;
        }

        public string CopyLog()
        {
            StringBuilder sb = new();
            foreach (var log in Logs.ToArray())
            {
                if (log.Message.Contains(AppConstants.DoubleClickMessage))
                {
                    continue;
                }
                sb.Append(log.Time.ToString("yyyy-MM-dd HH:mm:ss"));
                sb.Append(' ');
                sb.Append(log.Message);
                sb.Append(Environment.NewLine);
            }
            return sb.ToString();
        }

        public void Info(string format, params object[] args)
        {
            if (AppManager.EXIT_APP) return; // 如果应用正在退出，则不记录日志
            App.Current.Dispatcher.BeginInvoke(
                new System.Action(() =>
                {
                    //要执行的代码
                    if (format != null)
                    {
                        Logs.Add(new LogParams(LogType.Info, string.Format(format, args)));
                    }
                })
            );
        }

        public void Warn(string format, params object[] args)
        {
            if (AppManager.EXIT_APP) return; // 如果应用正在退出，则不记录日志
            App.Current.Dispatcher.BeginInvoke(
                new System.Action(() =>
                {
                    //要执行的代码
                    if (format != null)
                    {
                        Logs.Add(new LogParams(LogType.Warning, string.Format(format, args)));
                    }
                })
            );
        }

        public void Error(Exception exception)
        {
            if (AppManager.EXIT_APP) return; // 如果应用正在退出，则不记录日志
            App.Current.Dispatcher.BeginInvoke(
                new System.Action(() =>
                {
                    //要执行的代码
                    if (exception != null)
                    {
                        Logs.Add(new LogParams(LogType.Error, exception.Message));
                    }
                })
            );
        }

        public void Error(string message)
        {
            if (AppManager.EXIT_APP) return; // 如果应用正在退出，则不记录日志
            App.Current.Dispatcher.BeginInvoke(
                new System.Action(() =>
                {
                    //要执行的代码
                    if (message != null)
                    {
                        Logs.Add(new LogParams(LogType.Error, message));
                    }
                })
            );
        }

        public int CompareTo(DownloadM3u8TaskItem? other)
        {
            return VideoName.CompareTo(other.VideoName);
        }

        public override string ToString()
        {
            return VideoName;
        }
    }
}
